package the.workshop.tms.order.interfaces.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.wisteria.framework.util.excel.ExcelUtil;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.BusinessException;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.workshop.tms.order.dto.OrderCondition;
import the.workshop.tms.order.infrastructure.persistent.model.OrderDO;
import the.workshop.tms.order.interfaces.req.OrderAddReq;
import the.workshop.tms.order.interfaces.req.OrderModifyReq;
import the.workshop.tms.order.interfaces.resp.OrderResp;
import the.workshop.tms.order.application.service.IOrderService;
import the.workshop.tms.order.application.service.OrderExcelService;
import the.workshop.tms.order.application.service.dto.OrderImpExcelDTO;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {
    @Resource
    private IOrderService orderService;

    @Resource
    private OrderExcelService orderExcelService;

    @PostMapping("/add")
    public APIResult<Boolean> add(@ModelAttribute OrderAddReq req) {
        return APIResult.success(orderService.save(req.toOrder()));
    }

    @PostMapping("/modify")
    public APIResult<Boolean> modify(@ModelAttribute OrderModifyReq req) {
        return APIResult.success(orderService.updateById(req.toOrder()));
    }

    @PostMapping("/remove")
    public APIResult<Boolean> remove(@RequestParam(value = "ids", required = true) List<Integer> ids) {
        return APIResult.success(orderService.removeByIds(ids, UserContext.userId(), UserContext.userName()));
    }

    @GetMapping("/{id}")
    public APIResult<OrderResp> get(@PathVariable(value = "id") Integer id) {
        OrderDO order = orderService.getById(id);
        if (order == null) {
            return APIResult.success();
        }
        OrderResp resp = new OrderResp();
        BeanUtils.copyProperties(order, resp);
        return APIResult.success(resp);
    }

    @GetMapping("/page")
    public APIResult<QueryResult<OrderDO>> page(@ModelAttribute OrderCondition condition) {
        return APIResult.success(orderService.pageList(condition, condition.getPageNum(), condition.getPageSize()));
    }

    @PostMapping("/excel/imp")
    public APIResult<Boolean> impExcel(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw BusinessException.withErrorCode("没有可导入的文件");
        }

        List<OrderImpExcelDTO> data = null;
        try {
            data = ExcelUtil.readXls(file.getBytes(), OrderImpExcelDTO.class);
        } catch (Exception e) {
            log.error("导入客户数据，文件解析异常。", e);
            throw BusinessException.withErrorCode("导入客户数据，文件解析异常");
        }

        return APIResult.success(orderExcelService.impExcel(data));
    }

}
